package com.a2j.app.user.controller;

import com.a2j.app.user.service.AppUserService;
import com.a2j.beans.base.BaseBean;
import com.a2j.beans.base.code.ResponseCode;
import com.a2j.beans.user.AppUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: bzb
 * Data: 2021/10/21 22:44
 * Desc: 用户模块接口
 */
@RestController
@RequestMapping("/appUser")
@Api(tags = "普通用户")
public class AppUserController {

    @Resource
    AppUserService userService;


    /**
     * 登录
     * `@PutMapping` 兼容POST和PUT
     * @param username 用户名
     * @param password 密码
     */
    @RequestMapping(value = "/login", method = {RequestMethod.PUT, RequestMethod.POST})
    @ApiOperation(value = "登录", notes = "用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户名", required = true, defaultValue = "bzb"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, defaultValue = "123")
    })
    public BaseBean<AppUser> login(@RequestParam("account") String username, @RequestParam String password) {
        return userService.login(username, password);
    }

    /**
     * 用户详情
     * @param id 用户id
     */
    @GetMapping("/userDetail/{id}")
    @ApiOperation(value = "用户详情")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int", defaultValue = "1")
    public BaseBean<AppUser> appUserDetail(@PathVariable Integer id) {
        return userService.appUserDetail(id);
    }

    /**
     * 开关
     * @param id 用户id
     * @param enable 开关状态  0:关 1:开
     */
    @PutMapping("/enable")
    @ApiOperation(value = "开关")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "enable", value = "开关状态  0:关 1:开", required = true, dataType = "int", defaultValue = "0")
    })
    public BaseBean<String> enable(@RequestParam Integer id, @RequestParam Integer enable) {
        userService.enable(id, enable);
        return BaseBean.success(ResponseCode.SUCCESS);
    }

    /**
     * 修改密码
     * @param id 用户id
     * @param oldPwd 旧密码
     * @param newPwd 新密码
     */
    @PutMapping("/changePassword")
    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "oldPwd", value = "旧密码", required = true),
            @ApiImplicitParam(name = "newPwd", value = "新密码", required = true)
    })
    public BaseBean<String> changePassword(
            @RequestParam Integer id,
            @RequestParam String oldPwd,
            @RequestParam String newPwd
    ) {
        userService.changePassword(id, oldPwd, newPwd);
        return BaseBean.success(ResponseCode.SUCCESS);
    }


    /**
     * 测试注册
     * @param username 用户名
     * @param password 密码
     * @param phone 手机号
     */
    @PostMapping("/registerTest")
    @ApiOperation(value = "测试注册", notes = "用于测试简单参数注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, defaultValue = "bzb"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, defaultValue = "123"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, defaultValue = "12312341234")
    })
    public BaseBean<String> registerTest(@RequestParam String username, @RequestParam String password, @RequestParam String phone) {
        userService.register(AppUser.builder().username(username).password(password).phone(phone).build());
        return BaseBean.success(ResponseCode.SUCCESS);
    }

    /**
     * 查询所有用户(测试)
     * @return 所有用户的列表
     */
    @GetMapping("/allUser")
    @ApiOperation(value = "查询所有用户(测试)", notes = "用于测试查询所有用户")
    public BaseBean<List<AppUser>> allUser() {
        return userService.allUser();
    }

    /**
     * web端注册
     * @param record 注册的用户信息
     */
    @PostMapping("/register")
    @ApiOperation(value = "web端注册", notes = "web端注册需要的参数比较完整")
    @ApiImplicitParams({})
    public BaseBean<String> register(@RequestBody AppUser record) {
        userService.register(record);
        return BaseBean.success(ResponseCode.SUCCESS);
    }

    /**
     * App端注册
     * @param username 用户名
     * @param password 密码
     * @param phone 手机号
     * @param idCard 身份证号
     */
    @PostMapping("/registerApp")
    @ApiOperation(value = "App端注册", notes = "app端注册只需要几个重要参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户名", required = true, defaultValue = "bzb"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, defaultValue = "123"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, defaultValue = "12312341234"),
            @ApiImplicitParam(name = "idCard", value = "身份证号"),
    })
    public BaseBean<String> registerApp(
            @RequestParam("account") String username,
            @RequestParam String password,
            @RequestParam String phone,
            @RequestParam(required = false) String idCard
    ) {
        Map<String, String> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        map.put("phone", phone);
        map.put("idCard", idCard);
        userService.register(map);
        return BaseBean.success(ResponseCode.SUCCESS);
    }

    /**
     * 修改用户信息
     * @param record 修改用户信息的参数
     */
    @PutMapping("updateUser")
    @ApiOperation(value = "修改用户信息")
    @ApiImplicitParams({})
    public BaseBean<String> updateUser(@RequestBody AppUser record) {
        // 涉及到token的安全校验
        // 如果是移动端，则要判读不能修改的字段
        userService.updateUser(record);
        return BaseBean.success(ResponseCode.SUCCESS);
    }
}
